/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function(root1, root2) {
  if(!root1 && !root2) return null;
  else if(!root1) return root2;
  else if(!root2) return root1;
  function preView(node1, node2) {
    if(!node1 && !node2) return ;
    if(node1 && node2) {
      node1.val = node1.val + node2.val;
    }

    // 左边
    if(node1.left && node2.left) {
      preView(node1.left, node2.left);
    } else if(!node1.left) {
      node1.left = node2.left;
    }
    // 右边
    if(node1.right && node2.right) {
      preView(node1.right, node2.right);
    } else if(!node1.right) {
      node1.right = node2.right;
    }
  }
  preView(root1, root2);
  return root1;
};
